//package com.zhuangjie.shop;
//
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.HashSet;
//import java.util.List;
//import java.util.concurrent.*;
//import java.util.concurrent.atomic.AtomicInteger;
//import java.util.concurrent.locks.Condition;
//import java.util.concurrent.locks.ReentrantLock;
//
//public class SimpleThreadPool implements Executor {
//    /**
//     * 下面都是一起配合使用的组合参数
//     * 解决了：
//     * 当前总线程个数、当前线程池状态
//     */
//    private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
//    private static final int COUNT_BITS = Integer.SIZE - 3;
//    private static final int CAPACITY   = (1 << COUNT_BITS) - 1;
//
//    // runState is stored in the high-order bits
//    private static final int RUNNING    = -1 << COUNT_BITS;
//    private static final int SHUTDOWN   =  0 << COUNT_BITS;
//    private static final int STOP       =  1 << COUNT_BITS;
//    private static final int TIDYING    =  2 << COUNT_BITS;
//    private static final int TERMINATED =  3 << COUNT_BITS;
//
//    // Packing and unpacking ctl
//    private static int runStateOf(int c)     { return c & ~CAPACITY; }
//    private static int workerCountOf(int c)  { return c & CAPACITY; }
//    private static int ctlOf(int rs, int wc) { return rs | wc; }
//
//    /**
//     * 判断是否处于运行状态
//     */
//    private boolean isRunning() {
//        return this.ctl.get() < SHUTDOWN;
//    }
//
//
//    /**
//     * 线程配合使用参数
//     * 解决了线程间的有序执行
//     * 解决了线程的等待、唤醒、超时
//     */
//    private final ReentrantLock mainLock = new ReentrantLock();
//    private final Condition condition = mainLock.newCondition();
//    // 打工人集合
//    private List<Worker> workers = new ArrayList<>();
//
//
//
//    // 任务队列（阻塞队列）
//    private BlockingQueue<Runnable> workQueue;
//    private volatile long keepAliveTime;
//    private volatile TimeUnit timeoutUnit;
//    private volatile int corePoolSize;
//    private volatile int maximumPoolSize;
//    private ThreadFactory threadFactory;
//    private RejectedExecutionHandler handler;
//
//
//
//    @Override
//    public void execute(Runnable command) {
//        mainLock.lock();
//        try {
//            int ctl = this.ctl.get();
//            if (! isRunning()) {
//                throw new RuntimeException("线程池已经关闭！");
//            }
//            if(workerCountOf(ctl) < corePoolSize && addWorker(command,true)) {
//                return;
//            }
//            if( workQueue.offer(command) ) {
//                if (isRunning() && workerCountOf(ctl =  this.ctl.get()) == 0) {
//                    addWorker(command,false);
//                }
//            }else if(workerCountOf(ctl) < maximumPoolSize) {
//                // 线程池已经满了,且未达到最大线程数量,创建未核心线程
//                addWorker(command,false);
//            }else {
//                reject(command);
//            }
//        }finally {
//            mainLock.unlock();
//        }
//    }
//
//    final void reject(Runnable command) {
//        handler.rejectedExecution(command, null);
//    }
//
//    private boolean addWorker(Runnable command, boolean core) {
//
//    }
//
//    private class Worker implements Runnable{
//        private Runnable command;
//        private Boolean core;
//        public Worker(Runnable command,Boolean core) {
//            this.command = command;
//            this.core = core;
//        }
//        @Override
//        public void run() {
//            mainLock.lock();
//            try {
//                while (true) {
//                    if(ctl.get() >= STOP || Thread.currentThread().isInterrupted()) {
//                        break;
//                    }
//                    if (this.command != null) {
//                        this.command.run();
//                    }
//                    Runnable queueCommand = workQueue.poll();
//                    try {
//                        if (queueCommand == null && condition.await(keepAliveTime,timeoutUnit)) {
//                            break;
//                        }
//                    }catch (Exception e) {
//                        // 被中断，继续工作从队列中继续取任务执行
//                    }
//                }
//            }finally {
//                mainLock.unlock();
//            }
//
//
//        }
//    }
//}
